package cn.ingenic.glasssync.language;

import android.content.Context;
import android.content.res.Configuration;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;
import android.os.Message;
import android.os.RemoteException;
import android.util.Log;
import cn.ingenic.glasssync.services.SyncData;
import cn.ingenic.glasssync.services.SyncException;
import cn.ingenic.glasssync.services.SyncModule;
import android.app.IActivityManager;
import android.app.ActivityManagerNative;
import android.app.backup.BackupManager;

import java.util.Locale;

public class LanguageModule extends SyncModule {
	private final String TAG = "LanguageModule";
	public static String SMS_NAME = "lang_module";
	private final String LANGUAGE_TYPE = "languageType";
        private final String SYNC_RESULT = "result";
        private final String SYNC_TYPE = "type";
        private final int  COMPLETE = 1;
        private final int DEFALUT_LANGUAGE = 2;
        private final int LANGUAGE_ZH = 0;
        private final int LANGUAGE_US = 1;
        private final int LANGUAGE_FR = 2;
        private final int LANGUAGE_RU = 3;
        private final int LANGUAGE_DE = 4;
        private final int LANGUAGE_TH = 5;
        private final int LANGUAGE_FA = 6;
        private final int LANGUAGE_ES = 7;
        private final int LANGUAGE_PT = 8;
        private final int LANGUAGE_AR = 9;
	private final int LANGUAGE_IT = 10;
        private final int UPDATE_FAILED = 0;
        private final int UPDATE_SUCCESS = 1;
        private Context mContext;
	private static LanguageModule mInstance;

	public static LanguageModule getInstance(Context context) {
		if (mInstance == null) {
			mInstance = new LanguageModule(context);
		}
		return mInstance;
	}

	private LanguageModule(Context context) {
		super(SMS_NAME, context);
		mContext = context;
	}

        @Override
	protected void onCreate() {

	}

        @Override
	protected void onRetrive(SyncData data) {
	    int type = data.getInt(LANGUAGE_TYPE);
	    Log.d(TAG,"onRetrive::type =" +type);
	    switch (type) {
	    case LANGUAGE_ZH:
		    updateLocale(new Locale("zh", "CN"),type);
		break;
	    case LANGUAGE_US:
		    updateLocale(new Locale("en", "US"),type);
		break;
	    case LANGUAGE_FR:
		    updateLocale(new Locale("fr", "FR"),type);
		break;
	    case LANGUAGE_RU:
		    updateLocale(new Locale("ru", "RU"),type);
		break;
	    case LANGUAGE_DE:
		    updateLocale(new Locale("de", "DE"),type);
		break;
	    case LANGUAGE_TH:
		    updateLocale(new Locale("th", "TH"),type);
		break;
	    case LANGUAGE_FA:
		    updateLocale(new Locale("fa", "IR"),type);
		break;
	    case LANGUAGE_ES:
		    updateLocale(new Locale("es", "ES"),type);
		break;
	    case LANGUAGE_PT:
		    updateLocale(new Locale("pt", "PT"),type);
		break;
	    case LANGUAGE_AR:
		    updateLocale(new Locale("ar", "AE"),type);
		break;
	    case LANGUAGE_IT:
		    updateLocale(new Locale("it", "IT"),type);
		break;
	    }
	}

        /**
	 * Requests the system to update the system locale. Note that the system looks halted
	 * for a while during the Locale migration, so the caller need to take care of it.
	 */
        private void updateLocale(Locale locale,int language) {
	    try {
		IActivityManager am = ActivityManagerNative.getDefault();
		Configuration config = am.getConfiguration();
		
		// Will set userSetLocale to indicate this isn't some passing default - the user
		// wants this remembered
		config.setLocale(locale);
		
		am.updateConfiguration(config);
		// Trigger the dirty bit for the Settings Provider.
		BackupManager.dataChanged("com.android.providers.settings");
		sendSyncResult(COMPLETE,UPDATE_SUCCESS,language);
	    } catch (RemoteException e) {
		Log.e(TAG,"exception = :" + e);
		sendSyncResult(COMPLETE,UPDATE_FAILED,language);
	    }
	}

	private void sendSyncResult(int type,int result,int language){
	    SyncData data = new SyncData();
	    data.putInt(SYNC_TYPE,type);
	    data.putInt(SYNC_RESULT, result);
	    data.putInt("language",language);
	    try {
		send(data);
	    } catch (SyncException e) {
		Log.e(TAG, "---send sync failed:" + e);
	    }
	}
}

